package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量增加
    @Override
    public void add(List<OrderSetting> list) {
        if(list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                editNumber(orderSetting);
            }
        }
    }

    @Override
    public List<Map> getOrdersettingByMonth(String date) {
        String begin = date+"-1";
        String end = date+"-31";
        List<Map> list = orderSettingDao.getOrdersettingByMonth(begin,end);
        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        editNumber(orderSetting);
    }


    @Override
    public List<OrderSetting> findAll(String date) {
        String begin = date+"-1";
        String end = date+"-31";
        List<OrderSetting> list = orderSettingDao.findAll(begin,end);
        return list;
    }

    public void editNumber(OrderSetting orderSetting){
        //查询这个日期在数据库中是否存在
        int count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        //数据库有 修改
        if(count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //数据库没有 增加
            orderSettingDao.add(orderSetting);
        }
    }
}
